package com.xwh.b2c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.repository.AbstractRepository;
import com.xwh.b2c.dto.GoodsPageQueryDTO;
import com.xwh.b2c.entity.Goods;
import com.xwh.b2c.exceptions.BusinessException;
import com.xwh.b2c.mapper.GoodsMapper;
import com.xwh.b2c.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author xoox
 * @since 2025-07-10
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public IPage<Goods> getPage(GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("impl: current:{}",goodsPageQueryDTO.getCurrent());
        log.info("impl: pageSize:{}",goodsPageQueryDTO.getPageSize());
        Page<Goods> pageInfo = new Page<>(goodsPageQueryDTO.getCurrent(), goodsPageQueryDTO.getPageSize());
        log.info("impl: goodsDTO:{}",goodsPageQueryDTO);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.hasText(goodsPageQueryDTO.getName()),Goods::getName,goodsPageQueryDTO.getName());
        wrapper.eq(StringUtils.hasText(goodsPageQueryDTO.getGoodsNo()), Goods::getGoodsNo, goodsPageQueryDTO.getGoodsNo());
        wrapper.eq(goodsPageQueryDTO.getCategoryId() != null, Goods::getCategoryId, goodsPageQueryDTO.getCategoryId());
        wrapper.eq(goodsPageQueryDTO.getBrandId() != null, Goods::getBrandId, goodsPageQueryDTO.getBrandId());
        IPage<Goods> page = this.page(pageInfo, wrapper);

        log.info("");
        return page;

//        List<Goods> records = page.getRecords();
//        List<GoodsPageVO> pageVOList = new ArrayList<>();
//        for (Goods record : records) {
//
//
//        }
    }

    @Override
    public void enable(Goods goods) {
        if(goods == null){
            throw new BusinessException("商品不存在");
        }
        Boolean updateStatus = !goods.getIsDel();
        goods.setIsDel(updateStatus);

        this.updateById(goods);
    }

}
